const app = getApp();

import req from "../../utils/request";
import { USER_Place_Order } from "../../messages/uris";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    order: {
      courierCode: "", // 取件码
      courierType: "", // 快递类型 (百事, 申通)
      dormitoryAddress: "", // 宿舍号(送达地址详细位置)
      expectDeliveryTime: "", // 期望送达时间
      goodsType: "", // 物品类型 (大件, 普通件, 小件)
      phoneTail: "", // 手机尾号
      remark: "", // 备注
    }, 
    //弹出层
    topTips: false,
    hide: false,
    tipValue: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    // TODO 获取用户信息
    // db.collection("user_info")
    //   .where({
    //     _openid: app.globalData.openid,
    //   })
    //   .get()
    //   .then((res) => {
    //     that.setData({
    //       phone: res.data[0].phone,
    //       deliver_address: res.data[0].address,
    //     });
    //   });
  },
  update_courierType: function (e) {
    this.data.order.courierType = e.detail.detail.value;
  },
  update_courierCode: function (e) {
    this.data.order.courierCode = e.detail.detail.value;
  },
  update_goodsType: function (e) {
    this.data.order.goodsType = e.detail.detail.value;
  },
  update_dormitoryAddress: function (e) {
    this.data.order.dormitoryAddress = e.detail.detail.value;
  },
  update_expectDeliveryTime: function (e) {
    this.data.order.expectDeliveryTime = e.detail.detail.value;
  },
  update_phoneTail: function (e) {
    this.data.order.phoneTail = e.detail.detail.value;
  },
  update_remark: function (e) {
    this.data.order.remark = e.detail.detail.value;
  },
  order_submit: function (e) {
    // 登陆校验
    if (app.globalData.login == false) {
      wx.showModal({
        title: "未注册",
        content: "注册后才能创建任务",
        showCancel: false,
      });
      return;
    }
    // 参数校验
    if (this.data.courierType == "") {
      wx.showModal({
        title: "错误",
        content: "请填写快递类型",
        showCancel: false,
      });
      return;
    }
    if (this.data.courierCode == "") {
      wx.showModal({
        title: "错误",
        content: "请填写取件码",
        showCancel: false,
      });
      return;
    }
    if (this.data.goodsType == "") {
      wx.showModal({
        title: "错误",
        content: "请填写物品类型",
        showCancel: false,
      });
      return;
    }
    if (this.data.dormitoryAddress == "") {
      wx.showModal({
        title: "错误",
        content: "请填写送货地址",
        showCancel: false,
      });
      return;
    }
    if (this.data.expectDeliveryTime == "") {
      wx.showModal({
        title: "错误",
        content: "请填写期望送达时间",
        showCancel: false,
      });
      return;
    }
    wx.showLoading({
      title: "请稍后",
      mask: "true",
    });
    this.postOrder()
  },

  /**
   * 发布订单
   */
  postOrder: function () {
    // 请求后端接口,
    req.post(
      USER_Place_Order,
      this.data.order,
      res=>{
        wx.showModal({
          title: "下单成功",
          content: "您已成功下单，快递订单保留时间为一天，您也可以手动取消",
          showCancel: false,
          success: function () {
            wx.switchTab({
              url: "../order_list/order_list",
            });
          },
        });
      },
      (res) => {
      },
      (res) => {
        wx.hideLoading();
      },that
    );
  },
});
